package com.ming.closeto.view;


import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.graphics.drawable.shapes.Shape;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;

import com.ming.closeto.utils.ScreenUitls;

import java.util.logging.LogRecord;

/**
 * 缓存扫描
 */
public class CacheSanView extends View {

    private static final int H_START_ANIM = 1000;

    private Paint mPaintLine;
    private Paint mPaintCircle;
    private Paint mPaintText;

    private String text = "0.0B";
    private boolean isAnimation;

    // 矩阵
    private Matrix matrix;

    // 旋转角度
    private int start;

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == H_START_ANIM) {
                // 执行动画
                start++;
                matrix.postRotate(start, ScreenUitls.width/2, ScreenUitls.height/2);
                invalidate();
                mHandler.sendEmptyMessageDelayed(H_START_ANIM, 50);

            }
            return false;
        }
    });

    public void setText(String text) {
        this.text = text;
    }

    public String getText() {
        return text;
    }

    public void setAnimation(boolean animation) {
        isAnimation = animation;
        if (isAnimation) {
            mHandler.sendEmptyMessage(H_START_ANIM);
        } else {
            mHandler.removeMessages(H_START_ANIM);
        }
    }

    public CacheSanView(Context context) {
        super(context);
        initView();
    }

    public CacheSanView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public CacheSanView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    // 初始化
    private void initView() {
        // 初始化 画笔
        mPaintLine = new Paint();
        mPaintLine.setColor(Color.BLACK);
        mPaintLine.setAntiAlias(true);
        mPaintLine.setStyle(Paint.Style.STROKE);

        // 初始化 圆
        mPaintCircle = new Paint();
        mPaintCircle.setColor(Color.RED);
        mPaintCircle.setAntiAlias(true);

        mPaintLine = new Paint();
        mPaintLine.setTextSize(100);

        matrix = new Matrix();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(ScreenUitls.width, ScreenUitls.height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 绘制文字
//        canvas.drawText(text, ScreenUitls.width/2, 200, mPaintText);

        // 绘制四个圆
        canvas.drawCircle(ScreenUitls.width/2, ScreenUitls.height/2, ScreenUitls.width/2, mPaintLine);
        canvas.drawCircle(ScreenUitls.width/2, ScreenUitls.height/2, ScreenUitls.width/3, mPaintLine);
        canvas.drawCircle(ScreenUitls.width/2, ScreenUitls.height/2, ScreenUitls.width/4, mPaintLine);
        canvas.drawCircle(ScreenUitls.width/2, ScreenUitls.height/2, ScreenUitls.width/6, mPaintLine);

        // 绘制渐变圆
        Shader shader = new SweepGradient(ScreenUitls.width/2, ScreenUitls.height/2, Color.TRANSPARENT, Color.parseColor("#FFFF4081"));
        // 绘制时渐变
        mPaintCircle.setShader(shader);
        // 前置动画
        canvas.concat(matrix);
        canvas.drawCircle(ScreenUitls.width/2, ScreenUitls.height/2, ScreenUitls.width/2, mPaintCircle);
    }
}
